/*
 *英才会硕信息科技有限公司拥有本软件的版权，拥有本软件版权2025   并保留所有权利。
 *Copyright 2021,YCHS Information&Science Technology Co.,Ltd,
 *All rights reserved.
 */
package com.ychs.lawyerback01.service.imp;

import com.ychs.lawyerback01.mapper.PrivilegeMapper;
import com.ychs.lawyerback01.pojo.PrivilegeEntity;
import com.ychs.lawyerback01.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xuehongjin
 * @version 1.0
 * @date 2025/8/22
 */
@Service
public class PrivilegeServiceImpl implements PrivilegeService {

    @Autowired
    private PrivilegeMapper privilegeMapper;

    @Override
    @Transactional
    public void saveRolePermissions(Long roleId, List<Long> menuIds) {
        // 删除原有权限
        privilegeMapper.deleteByRoleId(roleId);

        // 批量添加新权限
        if (menuIds != null && !menuIds.isEmpty()) {
            List<PrivilegeEntity> privileges = menuIds.stream()
                    .map(menuId -> {
                        PrivilegeEntity privilege = new PrivilegeEntity();
                        privilege.setRoleId(roleId);
                        privilege.setMenuId(menuId);
                        return privilege;
                    })
                    .collect(Collectors.toList());
            privilegeMapper.batchInsert(privileges);
        }
    }

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        return privilegeMapper.getMenuIdsByRoleId(roleId);
    }
}
